<template>
  <div>
    <el-form ref="form" :model="messageForm" label-width="100px">
      <el-form-item label="消息类型">
        <el-select v-model="messageForm.news_type" placeholder="请选择发送类型">
          <el-option
                  v-for="item in sendType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <template v-if="messageForm.news_type === 1">
        <el-form-item label="我的建议">
          <el-input v-model="messageForm.news_content"
                    type="textarea"></el-input>
        </el-form-item>
      </template>
      <template v-if="messageForm.news_type === 0">
        <el-form-item label="上传文件">
          <upload :type="2" :is-edit="false" @getImgUrl="getFileUrl"/>
        </el-form-item>
      </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('cancelSend')">取消</el-button>
      <el-button type="primary" @click="send()">发送</el-button>
    </div>
  </div>
</template>

<script>
  import Upload from "./Upload";
  import axios from "axios";
  export default {
    name: "SendMessage",
    components:{
      Upload
    },
    props:{
      messageForm : Object,
      path : String
    },
    data(){
      return{
        sendType : [{
          label : '建议',
          value : 1,
        },
          {
            label: '文件',
            value: 0
          }]
      }
    },
    methods:{
      send(){
        this.sendMessage = false
        let url
        if(this.$store.getters.getUserType == 'server')
        {
          url = 'https://haotry.top/api/serviceSide/sendNotification'
        }
        else{
          url = 'https://haotry.top/api/buyer/sendNews'
        }
        axios.post(url, JSON.stringify(this.messageForm), {
          headers: {
            'Content-Type': 'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if(data.status === 200)
          {
            this.$message({
              type: 'success',
              message: '消息发送成功!'
            });
            this.$emit('cancelSend')
            this.$router.push(this.path)
          }
          else{
            this.$message({
              type: 'info',
              message: '消息发送失败!'
            });
          }
        })
      },
      getFileUrl(url){
        this.messageForm.news_content = url
      }
    }
  }
</script>

<style scoped>
  .dialog-footer{
    display: flex;
    justify-content: center;
  }
</style>